package com.okq.quartz;

import com.okq.entity.Notice;
import com.okq.service.INoticeService;
import com.okq.util.TimeTool;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 描述 : 公告通知定时器
 * 作者 : zdl
 * 日期 : 2017/7/11 10:10
 */
public class NoticeTimer {

    /**
     * 公告通知接口
     */
    @Autowired
    private INoticeService noticeServiceImpl;
    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(NoticeTimer.class);

    /**
     * 定时修改公告通知发布状态和逾期状态
     */
    public void modifyNotice() {
        try {
            List<Notice> nList = noticeServiceImpl.listNoOverdueNotice();
            if (!nList.isEmpty() && nList.size() > 0) {
                for (Notice n : nList) {
                    //是否已发布
                    Integer isRelease = n.getIsRelease();
                    String releaseTime = n.getReleaseTime();
                    Date nowDate = new Date();
                    boolean needUpdate = false;
                    if (isRelease == 0) {//未发布的公告通知，自动更新发布状态
                        if (StringUtils.isNotBlank(releaseTime)) {
                            Date releaseDate = TimeTool.stringToDate(releaseTime, "yyyy-MM-dd HH:mm:ss");
                            if (releaseDate.getTime() <= nowDate.getTime()) {
                                n.setIsRelease(1);
                                needUpdate = true;
                            }
                        }
                    }
                    //自动更新逾期状态
                    String overdueTime = n.getOverdueTime();
                    if (StringUtils.isNotBlank(overdueTime)) {
                        Date overdueDate = TimeTool.stringToDate(overdueTime, "yyyy-MM-dd HH:mm:ss");
                        if (overdueDate.getTime() <= nowDate.getTime()) {
                            n.setOverdue(1);
                            needUpdate = true;
                        }
                    }
                    if (needUpdate) {
                        noticeServiceImpl.updateNotice(n);
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }
}
